package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"gin-example/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"log"
	"net/http"
	"strconv"
)

func SendVerifyCode(c *gin.Context) {
	telephone := c.Query("telephone")
	valid1 := validation.Validation{}
	valid1.Required(telephone, "telephone").Message("telephone不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.SUCCESS
		util.SendSMS(telephone)
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]interface{}),
	})
}

func LoginByVC(c *gin.Context) {
	json := make(map[string]string)
	data := make(map[string]interface{})
	_ = c.BindJSON(&json)

	telephone := json["telephone"]
	verifyCode := json["verifyCode"]
	valid1 := validation.Validation{}
	valid1.Required(telephone, "telephone").Message("telephone不能为空")
	valid1.Required(verifyCode, "verifyCode").Message("verifyCode不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.ERROR_VERIFYCODE_NOT_MATCH
		if util.CheckVerifyCode(telephone, verifyCode) {
			code = e.ERROR_NOT_EXIST
			if models.ExistUserByTelephone(telephone) {
				user := models.GetUserByTelephone(telephone)
				code = e.SUCCESS
				data["user"] = user
				data["token"] = user.ID
			}
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func LoginByPW(c *gin.Context) {
	json := make(map[string]string)
	data := make(map[string]interface{})
	_ = c.BindJSON(&json)

	telephone := json["telephone"]
	password := json["password"]
	valid1 := validation.Validation{}
	valid1.Required(telephone, "telephone").Message("telephone不能为空")
	valid1.Required(password, "password").Message("password不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.ERROR_NOT_EXIST
		if models.ExistUserByTelephone(telephone){
			code = e.ERROR_PASSWORD_NOT_MATCH
			user, b := models.Login(telephone, password)
			if b {
				code = e.SUCCESS
				data["user"] = user
				data["token"] = user.ID
			}
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetAllUsers(c *gin.Context) {
	data := make(map[string]interface{})
	code := e.SUCCESS

	data["users"] = models.GetAllUsers()

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func Register(c *gin.Context) {
	json := make(map[string]string)
	_ = c.BindJSON(&json)

	avatar := 1
	des := "还没有什么简介=="
	username := json["username"]
	password := json["password"]
	telephone := json["telephone"]
	verifyCode := json["verifyCode"]
	valid1 := validation.Validation{}
	valid1.Required(username, "username").Message("username不能为空")
	valid1.Required(password, "password").Message("password不能为空")
	valid1.Required(telephone, "telephone").Message("telephone不能为空")
	valid1.Required(verifyCode, "verifyCode").Message("verifyCode不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		if models.ExistUserByTelephone(telephone) {
			code = e.ERROR_TELE_EXIST
		} else if !util.CheckVerifyCode(telephone, verifyCode) {
			code = e.ERROR_VERIFYCODE_NOT_MATCH
		} else {
			code = e.SUCCESS
			models.AddUser(username, password, telephone, des, avatar)
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
		log.Println(json)



	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func UpdateUser(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS

	if models.ExistUserByID(id) {
		code = e.SUCCESS
		json := make(map[string]interface{})
		_ = c.BindJSON(&json)
		avatar := json["avatar"]
		if avatar != nil {
			tmp1, _ := strconv.Atoi(avatar.(string))
			json["avatar"] = tmp1
		}
		models.UpdateUser(id, json)
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func DeleteUser(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS
	if models.ExistUserByID(id) {
		models.DeleteUser(id)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}